package com.observer.custom;

import java.util.ArrayList;
import com.observer.custom.inter.Observer;
import com.observer.custom.inter.Subject;

/**
 * @ClassName: WeatherData
 * @Author: ZhangYue26
 * @Description: 被观察者对象，实现主题接口
 * @Date: 2020-09-17
 */
public class WeatherData implements Subject {

    private float temperature;//温度
    private float humidity;//湿度
    private float airpressure;//气压

    private ArrayList<Observer> observerArrayList;//观察者列表

    public WeatherData(){
        this.observerArrayList = new ArrayList<>();
    }

    @Override
    public void addObserver(Observer observer) {
        this.observerArrayList.add(observer);
    }

    @Override
    public void deleteObserver(Observer observer) {
        int i;
        if ((i = observerArrayList.indexOf(observer)) != -1) {
            observerArrayList.remove(i);
        }
    }

    @Override
    public void notifyAllObserver() {
        for (Observer observer : observerArrayList) {
            observer.update(this.temperature,this.humidity,this.airpressure);//通知观察者
        }
    }

    /**
      * @Description 被观察者数据发生改变
      * @author ZhangYue26
      * @date 2020/9/17
      * @param [temperature, humidity, airpressure]
      * @return void
      **/
    public void setMeasurements(float temperature,float humidity,float airpressure) {
        this.temperature = temperature;
        this.humidity = humidity;
        this.airpressure = airpressure;
        notifyAllObserver();//修改后通知观察者
    }


}
